home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c
- Subject: Re: gets() question
- Date: Mon, 08 Jan 96 13:51:56 GMT
- Organization: none
- Message-ID: <821109116snz@genesis.demon.co.uk>
- References: <4cosgf$rir@newsbf02.news.aol.com> <4cqkt8$1quo@news.gate.net>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <4cqkt8$1quo@news.gate.net> bhutto@gate.net "William Hutto" writes:
-
- >In article <4cosgf$rir@newsbf02.news.aol.com>,
-
- >>#include "stdio.h"
-
- Should be:
-
- #include <stdio.h>
-
- if you want to be sure to include the standard system stdio.h header.
-
- >>void main(void)
-
- Read the FAQ.
-
- >>{
- >> int i[5];
- >> char s[3][10];
- >>
- >> printf ("Enter s[0]: ");
- >> gets (s[0]);
- > ^^^^
- >
- >Your compiler didn't generate an error?
-
- Why should it?
-
- >*s* is not an array of pointers to
- >arrays of chars as in argv[].
-
- True, s is an array of arrays of char. s[0] is an array of 10 chars.
- When used as an rvalue in an expression an array evaluates to a pointer to
- its first element i.e. a char * in this case.
-
- >Now I remember why I always like being explicit.
- >Try this:
- >
- > fgets(&s[0][0],9,stdin);
-
- It is certainly better to use fgets rather than gets but you could have
- written:
-
- fgets(s[0],sizeof s[0],stdin);
-
- s[0] is equivalent to &s[0][0] in this context and is arguably more
- readable. The length argument of fgets specifies the size of the array
- available in the first argument (which is 10 in this case) i.e. fgets will
- read a maximum of N-1 characters from the input stream and then terminate the
- sequence in the array with '\0'.
-
- >You can use a 2 dimensional char array, except that you have to be complete
- >in your form and s[0] is not. Because fgets() (or gets() for that matter) is
- >looking for an address, you need to oblige, hence the prefix of *&*.
-
- You need to read up on how arrays and pointers work in C. There are no such
- thing as 2 dimensional arrays, just arrays of arrays. Each element of the
- overall array is itself an array and behaves just like any other array.
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-